package com.acme.clientapp;

import java.net.*;

import java.io.*;

public class ClientApplication {
	static Socket requestSocket;
	static ObjectOutputStream outert;
	static ObjectInputStream in;
	static String message;

	public static void main(String[] args) {
		try {
			requestSocket = new Socket("localhost", 5050);
			System.out.println("Connected to localhost in port 2004");
			outert = new ObjectOutputStream(requestSocket.getOutputStream());
			outert.flush();
			in = new ObjectInputStream(requestSocket.getInputStream());
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String line = new String("");
			do {
				try {
					line = br.readLine();
					sendMessage("addClient");
					message = "C:\\workspace\\DB_Labs\\lab-13\\file.txt";
					sendMessage(message);
					message = (String) in.readObject();
					System.out.println("server>" + message);
				} catch (ClassNotFoundException classNot) {
					System.err.println("data received in unknown format");
				}
			} while (!line.equals("exit"));
		} catch (UnknownHostException unknownHost) {
			System.err.println("You are trying to connect to an unknown host!");
		} catch (IOException ioException) {
			ioException.printStackTrace();
		} finally {
			try {
				in.close();
				outert.close();
				requestSocket.close();
			} catch (IOException ioException) {
				ioException.printStackTrace();
			}
		}

	}

	static void sendMessage(final String msg) {
		try {
			outert.writeObject(msg);
			outert.flush();
			System.out.println("client>" + msg);
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}

}
